package game.roles.gamer;

import com.javaGame.util.MusicUtil;
import game.frame.MainFrame;
import game.roles.bullets.RoundBoom;
import game.roles.Role;
import game.roles.bullets.EnemyBullet;
import game.roles.enemys.EnemyRole;
import game.roles.gifts.Gift1;
import game.roles.visual.BackgroundMaps;
import game.utils.PressListenerOne;
import game.utils.ImageChange;
import game.utils.PressListenerTwo;

import java.awt.*;
import java.util.Random;

/**
 * @author liuyulai
 * Created with IntelliJ IDEA.
 * Date: 21.5.6
 * Time: 20:15
 * Description: 角色
 */
public class Gamer extends BaseGamer {
    /**
     * 前进角色图片数组
     */
    public static Image[] gamerArrayFrontNormal;

    /**
     * 前进无敌图片数组
     */
    public static Image[] gamerArrayFront;

    /**
     * 后退角色图片数组
     */
    public static Image[] gamerArrayBackNormal;

    /**
     * 后退无敌图片数组
     */
    public static Image[] gamerArrayBack;

    /**
     * 旋转数组
     */
    public static Image[] gamerSpin;


    /**
     * 是否为无敌状态
     */
    public boolean isInvincible = true;

    /**
     * 状态
     */
    private int statues = 0;

    /**
     * 初始状态
     */
    public static final int GAMER_INIT = 0;

    /**
     * 前进状态
     */
    public static final int GAMER_FRONT = 1;

    /**
     * 后退状态
     */
    public static final int GAMER_BACK = 2;

    /**
     * 旋转
     */
    public static final int GAMER_SPIN = 3;

    /**
     * 死亡状态
     */
    public static final int GAMER_DEAD = 4;

    /**
     * 无敌时间计数器
     */
    private int count;

    /**
     * 图片工具
     */
    private ImageChange rm = new ImageChange(2);

    public Gamer() {
        super(-100, 500, 120, 90);
    }


    /**
     * 友军横板坦克数量
     */
    public static int friendlyTankNum = 10;

    /**
     * 友军直升机数量
     */
    public static int friendlyHelicopterNum = 10;

    /**
     * 判断是否旋转
     */
    private boolean flag = true;

    /**
     * 切换冰子弹
     */
    public static boolean iceBullet = false;

    /**
     * 礼物效果1,允许三行开火
     */
    public static boolean allowFireByThree = false;

    @Override
    public void drawRole(Graphics graphics) {
        super.drawRole(graphics);
        if (MainFrame.sp >= 100) {
            MainFrame.sp = 100;
        }
    }

    @Override
    public void rolesMove() {
//        初始状态，大招旋转状态，死亡状态以及正常行动后被击中状态
        if (statues == GAMER_INIT) {
            this.setInit();
        } else if (statues == GAMER_SPIN) {
//            this.setSpin();
            this.setGamerMove();
        } else if (statues == GAMER_DEAD) {
            this.setDead();
        } else {
            this.setGamerMove();
            this.setByHit();
        }
        this.setImage();
    }

    /**
     * 设置玩家移动
     */
    @Override
    protected void setGamerMove() {
        if (isInvincible) {
            count++;
            if (count >= 40) {
                isInvincible = false;
                count = 0;
            }
        }
        if (PressListenerOne.isRight || PressListenerTwo.isRight) {
            this.x += 10;
            /**正常状态下角色移动范围受限制*/
            if (this.x >= 1200 && BackgroundMaps.mapMoveOrNot) {
                this.x = 1200;
            }
            /**
             * 角色前进时地图相应的往后进行输出
             */
            if (BackgroundMaps.mapMoveOrNot) {
                BackgroundMaps.mapX -= 2;
            }
            this.statues = GAMER_FRONT;
        } else if (PressListenerOne.isLeft || PressListenerTwo.isLeft) {
            this.x -= 10;
            if (this.x <= 0) {
                this.x = 0;
            }
            this.statues = GAMER_BACK;
        }

        if (PressListenerOne.isUp || PressListenerTwo.isUp) {
            this.y -= 10;
            if (this.y <= 360) {
                this.y = 360;
            }
        } else if (PressListenerOne.isDown || PressListenerTwo.isDown) {
            this.y += 10;
            if (this.y >= 500) {
                this.y = 500;
            }
        }
//        if (!KeyPressListener.isLeft && !KeyPressListener.isRight) {
//            this.statues = GAMER_FRONT;
//        }


    }

    /**
     * 放鸵鸟屁
     */
    public void gamerSendBoom() {
        if (MainFrame.boomNum <= 0) {
            return;
        }
        MainFrame.boomNum--;
        this.statues = GAMER_SPIN;
        for (int i = 0; i < 75; i++) {
            MainFrame.roleList.add(new RoundBoom(this, 0));
            MainFrame.roleList.add(new RoundBoom(this, 1));
            MainFrame.roleList.add(new RoundBoom(this, 2));
            MainFrame.roleList.add(new RoundBoom(this, 3));
        }
    }

    /**
     * 设置图片显示状态
     */
    protected void setImage() {
        if (statues == GAMER_INIT || statues == GAMER_FRONT && isInvincible == true) {
            this.img = this.rm.change(gamerArrayFront);
        } else if (statues == GAMER_FRONT && isInvincible == false) {
            this.img = this.rm.change(gamerArrayFrontNormal);
        } else if (statues == GAMER_BACK && isInvincible == false) {
            this.img = this.rm.change(gamerArrayBackNormal);
        } else if (statues == GAMER_BACK && isInvincible == true) {
            this.img = this.rm.change(gamerArrayBack);
        } else if (statues == GAMER_SPIN) {
            this.img = this.rm.change(gamerSpin);
        } else if (statues == GAMER_DEAD) {
            this.img = this.rm.change(gamerDead);
        }
    }

    /**
     * 设置死亡状态
     */
    @Override
    protected void setDead() {
        this.y += 20;
        //让玩家掉出场地
        if (this.y >= 800) {
            MainFrame.roleList.remove(this);
            MainFrame.gamerNum--;
            if (MainFrame.gamerNum < 0) {
                MainFrame.isPass = false;
                MainFrame.flag = false;
            } else {
                Gamer gamer = new Gamer();
                MainFrame.roleList.add(MainFrame.gamer = gamer);
            }
        }

    }


    /**
     * 设置初始状态
     */
    @Override
    protected void setInit() {
        this.x += 10;
        if (this.x >= 100) {
            statues = GAMER_FRONT;
        }
    }

    /**
     * 受击方法(此受击方法包括了被礼物击中)
     */
    @Override
    protected void setByHit() {
        for (int i = 0; i < MainFrame.roleList.size(); i++) {
            Role r = MainFrame.roleList.get(i);
            if (!isInvincible && this.rec.intersects(r.rec) && (r instanceof EnemyRole || r instanceof EnemyBullet)) {
                this.statues = GAMER_DEAD;
                this.setDead();
                return;
            } else if (r instanceof Gift1 && this.rec.intersects(r.rec)) {
                new MusicUtil("music/getGift.mp3", false);
                gamerHitByGift();
                MainFrame.roleList.remove(r);
            }
        }
    }

    /**
     * 礼物效果
     */
    @Override
    protected void gamerHitByGift() {
        //生成0-10的随机数
        int random = new Random().nextInt(10);

        switch (random) {
            //樱花炸弹数量+0到3个
            case 0:
                MainFrame.boomNum += new Random().nextInt(3);
                break;
            //允许玩家用三行子弹进行射击
            case 1:
                allowFireByThree = true;
                break;
            //玩家生命数量+1到5
            case 2:
                MainFrame.gamerNum += new Random().nextInt(5) + 1;
                break;
            //玩家的坦克道具数量+1到5
            case 3:
                friendlyTankNum += new Random().nextInt(5) + 1;
                break;
            case 4:
                friendlyHelicopterNum += new Random().nextInt(5) + 1;
                break;
            //切换子弹
            case 5:
                iceBullet = true;
                break;
            //切换回蓝色子弹
            case 6:
                iceBullet = false;
                break;
            //一半的几率为增加玩家的分数
            default:
                MainFrame.playerExp += 2000;
        }
    }

    public int getStatues() {
        return statues;
    }

    public void setStatues(int statues) {
        this.statues = statues;
    }
}
